package com.fencing_squad.homo.util;

import com.alibaba.fastjson.JSONObject;

public class Data {
    public static class Account {
        public static String token = "";
        public static boolean isLogin = false;
        public static String nickname = "";
        public static String uid = "";
        public static String avatar = "";
        public static double money = 0;
        public static int point = 0;
        public static String phone = "";
        public static String dormitory = "";
        public static String status = "";
        public static int postMessageNum = 0;
        public static int rewardNum = 0;

        public static void getFromJSON(JSONObject data) {
            JSONObject user = data.getJSONObject("user");
            nickname = user.getString("nick_name");
            uid = user.getString("user_id");
            avatar = data.getString("avatar");
            money = user.getDouble("money");
            point = user.getIntValue("point");
            phone = user.getString("phone");
            dormitory = user.getString("dormitory");
            status = user.getString("status");
            postMessageNum = data.getIntValue("post_message_num");
            rewardNum = data.getIntValue("reward_num");
        }

        public static void clear() {
            token = "";
            isLogin = false;
            nickname = "";
            uid = "";
            avatar = "";
            money = 0;
            point = 0;
            phone = "";
            dormitory = "";
            status = "";
            postMessageNum = 0;
            rewardNum = 0;
        }

        public static void validToken() {
            Network.get("/user", Account::resolve, null);
        }

        private static void resolve(JSONObject object) {
            getFromJSON(object.getJSONObject("data"));
        }
    }
}
